$(document).ready(function(){
	
	/**
	 * 查询
	 */
	var organIds = new Array();
	$("#datagrid-toolbar #search_link").linkbutton({
		onClick:function(){
			search(new page());
		}
	});
	
	/**
	 * 添加公告
	 */
	$("#datagrid-toolbar #add_link").linkbutton({
		onClick:function(){
			add();
		}
	});
	
	/**
	 * 浏览公告
	 */
	$("#datagrid-toolbar #view_link").linkbutton({
		onClick:function(){
			view();
		}
	});
	
	/**
	 * 删除公告
	 */
	$("#datagrid-toolbar #delete_link").linkbutton({
		onClick:function(){
			deleteAnnounce();
		}
	});
	
	/**
	 * 上传附件
	 */
	$("#datagrid-toolbar #upload_link").linkbutton({
		onClick:function(){
			upload();
		}
	});
	drawer()
	search(page_);
});

//设置全局的分页
var page_ = new page({search:search});

/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid:'#datagrid',
	datagridtoolbar:'#datagrid-toolbar', 
	singleSelect:true});

/**
 * 查询
 * @param page
 */
function search(pg){
	var url = ctx + "/announces/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}

/**
 * 获取查询条件
 * @returns {condition}
 */
function getCondition(){
	var condition = $("#datagrid-toolbar #listform").serializeJson();
	return condition;
}
/**
 * 查抽屉表
 * 查公告类型
 */
function drawer(){
	$.ajax({
	        url : ctx+"/drawer/findByParentCode",
  	        data : {pcode:1018000000000000},
  	        dataType : 'json',
  	        type : 'post',
  	        success : function (data) {
  	        	if(!showMsg(data)){
  					return;
  				}
  					$('#search_announceType').combobox({ 
  						valueField:'id',
	                    textField:'text',
	                    data:data,
	                    panelHeight:'auto'
  					});
  	    }}
			
	);
}

/**
 * 添加事件实现
 */
function add(){

	$('#dialog').dialog({
	    title: '添加公告',
	    width: 800,
	    height: 400,
	    left:250,
	    top:60,
	    href: 'add.jsp',
	    closed: false,
	    cache: false,
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    	var id = $('#id').val();
	    	organTree(id);
	    },
	    modal: true,
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var node = $('#organTree').tree('getChecked');

 			    if(node.length == 0){
 					message("请选中机构");
 					return;
 				}
 				var organIds = new Array();
 				for(var i=0; i<node.length; i++){
					organIds.push(node[i].id);
 				}
 				
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	addAnnounces(organIds);
            },
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

function addAnnounces(organIds){
	var data = $("#addform").serializeJson();
	data.organIds = organIds.join(",");
	$.ajax({
		type : "POST",
		url : ctx + "/announces/create",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"添加成功")){
					return;
			}
			$('#dialog').dialog('close');
			search(page_);
			
		}
	});
}
/**
 * 生成机构树
 */
function organTree(id) {
	$('#organTree').tree({
		animate : true,
		lines : true,
	    idField: 'id',
	    checkbox:true,
		url : ctx + "/tree/organ?id=" + id,	
		onLoadSuccess : function(node, data) {
			var node = $('#organTree').tree('find', id);
			$('#organTree').tree('select', node.target);
		}
	});
}
function upload(){

	var row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	var url = ctx + '/web/case/upload/fileupload.html?id='+row.id;
	if(row){
		var content = '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="yes"></iframe>';
	    var win = $('#dialogs').dialog({
	        content: content,
	        title: '上传附件',
		    width: 780,
		    height: 380,
		    left:150,
		    top:60,
	        modal: true,
	        cache: false
	    });

	    win.dialog('open').dialog('refresh');
	}
		
}
/**
 * 浏览
 */
function view(){
	var row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '浏览公告',
	    width: 600,
	    height: 400,
	    left:250,
	    top:60,
	    href: 'view.jsp',
	    closed: false,
	    cache: false,
	    onLoad:function(){
	    	show(row);
	    },
	    modal: true,
	    buttons:[{
			text:'返回',
			handler:function(){
				$('#dialog').dialog('close');
				}
			}
           ]
	});
}
function show(row){
	var id = row.id;
	$.ajax({
		type : "post",
		url : ctx + "/announces/detail",
		dataType : "json",
		data : {id:id},
		success : function(data) {
			if(!showMsg(data)){
				return;
		}
			if (data.result == "success") {
		    	$.each(row,function(key,value){
		    		$("#addForm" + " span[id="+key+"]").each(function(index,item){
						$(this).text(value);
					});
		    	});	
		    	loadFile(row.id);
		}
			
		}});
}

function loadFile(id){
	$.ajax({
        type: "GET",
        url:ctx+"/document/query",
        dataType:"json",
        data:{fid:id},
        success: function(data){
        	if(!showMsg(data)){
				return;
        	}
        	if (data.result == "success") {
        		$('#attachment').datagrid({
        			fitColumns:true,
       			    singleSelect:true,
       			    data:data.content
        		});
			}
        }
    });
}

function deleteAnnounce(){
	var row = getSelectedRow("#datagrid");
	if(row){
		$.messager.confirm('提示', '确定删除?', function(r){
	        if (r){
	       	 $.ajax({
	      	        url : ctx+"/announces/delete",
	      	        data : {id:row.id},
	      	        dataType : 'json',
	      	        type : 'post',
	      	        success : function (data) {
	      	        	if(!showMsg(data,"删除成功")){
	    					return;
	      	        	}
      					search(page_);
      					$('#dialog').dialog('close');
	      	    }});
	        }
	    });
	}
}